// 2025/3/6
// https://leetcode.cn/problems/set-matrix-zeroes/description/?envType=study-plan-v2&envId=top-100-liked
// 矩阵置零

class Solution {
    public:
        void setZeroes(vector<vector<int>>& matrix) {
            int m = matrix.size(), n = matrix[0].size();
            bool flagr = false, flagc = false;
            for(int i = 0; i < m; i++)
            {
                if(matrix[i][0] == 0)
                    flagr = true;
            }
            for(int j = 0; j < n; j++)
            {
                if(matrix[0][j] == 0)
                    flagc = true;
            }
    
            for(int i = 1; i < m; i++)
            {
                for(int j = 1; j < n; j++)
                {
                    if(matrix[i][j] == 0)
                    {
                        matrix[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                }
            }
    
            for(int i = 1; i < m; i++)
            {
                for(int j = 1; j < n; j++)
                {
                    if(matrix[i][0] == 0 || matrix[0][j] == 0)
                    {
                        matrix[i][j] = 0;
                    }
                }
            }
            
            if(flagr)
            {
                for(int i = 0; i < m; i++)
                    matrix[i][0] = 0;
            }
            if(flagc)
            {
                for(int j = 0; j < n; j++)
                    matrix[0][j] = 0;
            }
        }
    };